/**
 * Dear programmer:
 * When I wrote this code, only god and I knew how it worked.
 * Now, only god knows it!
 *
 * @author: Pegasus  Date: 2018/11/21 Time: 10:06
 *
 * 个人中心
 */

import React, {Component} from 'react';
import {
    Linking,
    SafeAreaView, ScrollView, TouchableOpacity, View, TouchableHighlight, Text, Platform, BackHandler, ToastAndroid,
} from 'react-native';
import {connect} from 'react-redux';
import {Row, Rows, Table} from 'react-native-table-component';
import {styles} from './StaffTimeSetting.Style';
import {wHSKShiftsSetupAdd} from "./Actions";
import {FetchStatus} from "../../../configuration/RequestConfiguration";
import {ReturnStatus} from "../../../configuration/ReturnStatus";
import {doDisplayWarnMessage} from "../../../component/Message/Action";
import {SCREEN_WIDTH} from "../../../constant/PHScreen";
import AntDesign from "react-native-vector-icons/AntDesign";
import SelectTimePlugIn from "../../custom/selectDateTime/selectTimePlugIn"
import BottomButtonView from "../../../component/BottomButtonView/BottomButtonView";


class StaffTimeSetting extends Component {

    static navigationOptions = ({navigation}) => ({
        headerTitle: `班次时间设置`,
    });

    // 构造
    constructor(props) {
        super(props);

        let date = new Date();
        let year = date.getFullYear().toString();
        let month = (date.getMonth()+1).toString();
        if(Number(month)<10){
            month = '0'+month
        }
        let day = date.getDate().toString();
        if(Number(day)<10){
            day = '0'+day
        }
        let nowDate = year+'-'+month+'-'+day;

        let whskShifts = this.props.navigation.getParam("whskShifts")?this.props.navigation.getParam("whskShifts"):[];
        let tableData = [
            ["早早班","00:00","至 当日","00:00"],
            ["早班","00:00","至 当日","00:00"],
            ["中班","00:00","至 当日","00:00"],
            ["晚班","00:00","至 次日","00:00"],
            ["备班","00:00","至 当日","00:00"],
        ];
        whskShifts.map((value,key)=> {
            let index = 0;
            switch (value.shift) {
                case '早早班': {
                    index = 0;
                    break;
                }
                case '早班': {
                    index = 1;
                    break;
                }
                case '中班': {
                    index = 2;
                    break;
                }
                case '晚班': {
                    index = 3;
                    break;
                }
                case '备班': {
                    index = 4;
                    break;
                }
            }
            tableData[index][1] = value.starttime;
            tableData[index][3] = value.endtime;
        })

        // 初始状态
        this.state = {
            whskShifts:whskShifts,
            opdate:this.props.navigation.getParam("opdate")?this.props.navigation.getParam("opdate"):nowDate,
            callBack:this.props.navigation.getParam("callBack"),
            widthArr:[70,(SCREEN_WIDTH-8-140)/2,70,(SCREEN_WIDTH-8-140)/2],
            tableData:tableData,
        };
    }

    componentWillReceiveProps(nextProps, nextContext): void {
        let {dispatch} = this.props;

        if (nextProps.onWHSKShiftsSetupAdd !== this.props.onWHSKShiftsSetupAdd && nextProps.onWHSKShiftsSetupAdd.fetchStatus === FetchStatus.Success) {
            if (nextProps.onWHSKShiftsSetupAdd.RETURN === ReturnStatus.Success) {
                ToastAndroid.show(nextProps.onLinen_save.MSG, 500);//提示
                this.props.navigation.state.params.callBack()
            } else {
                dispatch(doDisplayWarnMessage(nextProps.onWHSKShiftsSetupAdd.MSG))
            }
        }
    }

    render() {
        let {dispatch} = this.props;
        let table_arr = JSON.parse(JSON.stringify(this.state.tableData));
        table_arr.map((item,index)=>{
            let starttime = item[1];
            let html1 = (
                <SelectTimePlugIn defaultTime={starttime} textStyle={styles.opdateViewStyle} dateBack={(date)=>{
                    let new_tableData = JSON.parse(JSON.stringify(this.state.tableData));
                    new_tableData[index][1] = date;
                    // this.setState({tableData : []});
                    this.setState({tableData : new_tableData})
                }} />
            );
            item[1] = html1;

            let endtime = item[3];
            let html2 = (
                <SelectTimePlugIn defaultTime={endtime} textStyle={styles.opdateViewStyle} dateBack={(date)=>{
                    let new_tableData = JSON.parse(JSON.stringify(this.state.tableData));
                    new_tableData[index][3] = date;
                    this.setState({tableData : new_tableData})
                }} />
            );
            item[3] = html2;
        });

        return (
            <SafeAreaView style={styles.container} >
                <ScrollView style={styles.body}>
                    <Table style={{marginLeft: 2}}>
                        <Rows data={table_arr} widthArr={this.state.widthArr} style={styles.tableHeader} textStyle={styles.tableText}/>
                    </Table>
                </ScrollView>
                <BottomButtonView title={'完成'} onSubmitClick={() => {
                    let shitf_arr = [];
                    let reg = new RegExp("-","g");
                    let opdate =(this.state.opdate.replace(reg,""));

                    this.state.tableData.map((item,index)=>{
                        if(item[1]!==item[3]){
                            let shift_json = {};
                            shift_json.opdate = opdate;
                            shift_json.shift = item[0];
                            shift_json.starttime = item[1];
                            shift_json.endtime = item[3];
                            shitf_arr.push(shift_json)
                        }
                    });
                    let str = {"whsk_shifts_setup":shitf_arr};
                    console.log(JSON.stringify(str))

                    let {dispatch} = this.props;
                    dispatch(wHSKShiftsSetupAdd(JSON.stringify(str)))
                }}/>
            </SafeAreaView>
        )
    };

}

const props = state => {
    return {
        onWHSKShiftsSetupAdd:state.onWHSKShiftsSetupAdd
    }
};

export default StaffTimeSetting = connect(props)(StaffTimeSetting);